package com.beiing.tianshuai.tianshuai.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;

import java.lang.ref.WeakReference;

/**
 * Created by Jinni on 2017/7/14 0014.
 */

public class CustomProgressDialog extends Dialog {

    private WeakReference<Context> mContext = new WeakReference<>(null);
    private volatile static CustomProgressDialog sDialog;
    private static OnDismiss mListener;

    public static void setListener(OnDismiss listener) {
        mListener = listener;
    }

    public CustomProgressDialog(Context context, CharSequence message) {
        super(context, R.style.CustomProgressDialog);

        mContext = new WeakReference<>(context);

        @SuppressLint("InflateParams")
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading_progress, null);
//        TextView tvMessage = (TextView) view.findViewById(R.id.tv_message);
//        if (!TextUtils.isEmpty(message)) {
//            tvMessage.setText(message);
//        }
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addContentView(view, lp);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (sDialog == null) {
            return;
        }
//        ImageView imageView = (ImageView) findViewById(R.id.loadingImageView);
//        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
//        animationDrawable.startActivityForResult();

    }

    public static synchronized void showLoading(Context context, CharSequence message, boolean cancelable) {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }

        if (context == null || !(context instanceof Activity)) {
            return;
        }
        sDialog = new CustomProgressDialog(context, message);
        sDialog.setCancelable(cancelable);
        sDialog.setCanceledOnTouchOutside(false);
        sDialog.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if (mListener != null) {
                    mListener.onDismissListener();
                }
            }
        });
        if (sDialog != null && !sDialog.isShowing() && !((Activity) context).isFinishing()) {
            sDialog.show();
        }
    }

    public static synchronized void stopLoading() {
        if (sDialog != null && sDialog.isShowing()) {
            sDialog.dismiss();
        }
        sDialog = null;
    }

    public static synchronized void setCancelListener(OnCancelListener listener) {
        sDialog.setOnCancelListener(listener);
    }

    public interface OnDismiss {
        void onDismissListener();
    }
}
